<template>
  <div class="bottom-menu">
    <CheckButton
      class="select-all"
      @checkBtnClick="checkBtnClick"
      v-model="isSelectAll"
    ></CheckButton>
    <span>全选</span>
    <span class="total-price">合计: ¥{{ totalPrice }}</span>
    <span @click="goShoping" class="buy-product"
      >去结算({{ checkedLength }})</span
    >
  </div>
</template>

<script>
import CheckButton from "components/content/checkButton/CheckButton";
import { mapGetters, mapActions } from "vuex";
import { random_No } from "common/utils";
export default {
  name: "BottomBar",
  components: {
    CheckButton,
  },
  computed: {
    ...mapGetters(["cartList", "cartLength"]),
    totalPrice() {
      const cartList = this.cartList;
      return cartList
        .filter((item) => {
          return item.checked;
        })
        .reduce((preValue, item) => {
          return preValue + item.count * item.price;
        }, 0)
        .toFixed(2);
    },
    checkedLength() {
      return this.cartList.filter((item) => item.checked).length;
    },
    //全选按钮的状态显示
    isSelectAll: function () {
      // return this.cartList.find((item) => item.checked === false) === undefined;
      return (
        this.cartList.length !== 0 &&
        this.cartList.every((item) => item.checked)
      );
    },
  },
  methods: {
    checkBtnClick: function () {
      // 1.判断是否有未选中的按钮
      // let isSelectAll = this.$store.getters.cartList.find(
      //   (item) => !item.checked
      // );

      // 2.有未选中的内容, 则全部选中
      if (!this.isSelectAll) {
        this.cartList.forEach((item) => {
          item.checked = true;
        });
      } else {
        this.cartList.forEach((item) => {
          item.checked = false;
        });
      }
    },
    // mapActions的映射关系
    ...mapActions(["order"]),
    goShoping() {
      //过滤出选中的商品如果长度等于0就弹框
      if (this.checkedLength === 0) {
        //自定义全局插件，可以传入两个参数，第一个参数为你要显示的弹窗信息，第二个参数为多少毫秒以后显示
        this.$toast("你还没添加商品");
      } else {
        //判断是否登录了，没有登录就跳转到登录页面
        if (!this.$store.state.user.username) {
          this.$router.push({
            path: "/auth",
            query: {
              run: "login",
            },
          });
          this.$toast("请先登录", 1500);
        } else {
          //清空勾选了的，留下未勾选的
          let arr = this.cartList.filter((v) => !v.checked);
          //同时要将结算成功的商品信息添加到订单信息中去
          let orderArr = this.cartList.filter((v) => v.checked);
          //制造订单数组
          let orders = [];
          for (let i = 0; i < orderArr.length; i++) {
            let order = {};
            //订单编号
            order.no = random_No();
            //下单时间
            order.create_time = new Date().toLocaleString();
            //商品名称
            order.name = orderArr[i].title;
            //商品数量
            order.count = orderArr[i].count;
            //商品价格
            order.price = orderArr[i].price * order.count;
            //将商品信息作为一个对象添加到数组中
            orders.push(order);
          }
          this.order({ arr, orders }).then((res) => {
            //结算成功
            this.$toast(res, 1500);
          });
        }
      }
    },
  },
};
</script>

<style scoped>
.bottom-menu {
  width: 100%;
  height: 44px;
  background-color: #eee;
  position: fixed;
  bottom: 49px;
  left: 0;
  box-shadow: 0 -2px 3px rgba(0, 0, 0, 0.2);
  font-size: 14px;
  color: #888;
  line-height: 44px;
  padding-left: 35px;
  box-sizing: border-box;
}

.bottom-menu .select-all {
  position: absolute;
  line-height: 0;
  left: 12px;
  top: 13px;
}

.bottom-menu .total-price {
  margin-left: 15px;
  font-size: 16px;
  color: #666;
}

.bottom-menu .buy-product {
  background-color: orangered;
  color: #fff;
  width: 100px;
  height: 44px;
  text-align: center;
  line-height: 44px;
  float: right;
}
</style>
